package com.cjg.winter.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjg.winter.common.CodeUtil;
import com.cjg.winter.common.R;
import com.cjg.winter.common.ResponseEnum;
import com.cjg.winter.entity.Admin;
import com.cjg.winter.entity.email;
import com.cjg.winter.mapper.AdminMapper;
import com.cjg.winter.service.AdminService;
import com.cjg.winter.service.impl.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 老饼干
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/winter/admin")

public class AdminController {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AdminService adminService;
    Integer key = 0;

    @GetMapping("/list")
    R list() {
        List<Admin> admins = adminMapper.selectList(null);
        admins.forEach((s) -> {
            System.out.print(s);
        });
        return R.ok().put("data", admins);

    }

    @DeleteMapping("/removeById/{id}")
    R removeById(@PathVariable Long id) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        int delete = adminMapper.delete(wrapper);
        return R.ok().put("data", delete);
    }


    @PostMapping("/login")
    R login(@RequestBody Admin admin) {
        System.out.println("======================");
        System.out.println(admin);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("email", admin.getEmail());
        wrapper.eq("password", admin.getPassword());
        Admin admin1 = adminMapper.selectOne(wrapper);
        //邮箱已存在
        System.out.println("======================");
        System.out.println(admin1);
        if (admin1 == null) {
            return R.setResult(ResponseEnum.ERROR10003);
        }
        return R.ok();
    }


    @PostMapping("/insert")
    R add(@RequestBody Admin admin) {
        System.out.println(admin);
        //判断 用户名是否相同相同拒绝
        Map<String, Object> map = new HashMap<>();
        map.put("name", admin.getName());
        List<Admin> admin1 = adminMapper.selectByMap(map);
        if (admin1 == null) {
            adminMapper.insert(admin);
            return R.ok();
        } else {
            return R.setResult(ResponseEnum.ERROR10001);
        }
    }

    @PostMapping("/sendMail")
    R mail(@RequestBody() email email) {

        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email.getEmail());
        Admin admin1 = adminMapper.selectOne(wrapper);

        if (admin1 == null) {

            Map<Integer, String> map = adminService.senMail(email.getEmail());
            String s = map.get(key);
            Admin admin = new Admin();
            admin.setEmail(email.getEmail());
            admin.setConfirmCode(s);
            admin.setActivationTime(LocalDateTime.now().plusMinutes(10));
            adminMapper.insert(admin);
        } else {
            return R.setResult(ResponseEnum.ERROR10002);
        }
        return R.ok();
    }


    @PostMapping("/insert1")
    R add1(@RequestBody Admin admin) {
        System.out.println("===============================================");
        System.out.println(admin);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("confirm_code", admin.getConfirmCode());
        //根据 确认码查询,用户
        Admin admin1 = adminMapper.selectOne(wrapper);
        Duration between = Duration.between(LocalDateTime.now(), admin1.getActivationTime());
        long l = between.toMinutes();
        //判断 确认码的失效
        if (l >= -10) {
            //插入用户名密码创建
            admin1.setName(admin.getName());
            admin1.setPassword(admin.getPassword());
            adminMapper.updateById(admin1);
            return R.ok();
        }
        return R.error();

    }


    @PostMapping("/update")
    R update1(@RequestBody Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("name", admin.getName());
        Admin admin1 = adminMapper.selectOne(wrapper);
        admin1.setPassword(admin.getPassword());
        admin1.setGmtModified(new Date());
        adminMapper.updateById(admin1);
        return R.ok();
    }

    @GetMapping("/img")
    public String image() {
        return "verify_code";
    }

}